using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using GameFramework.Graphics;

namespace GameFramework.Controls
{
    public class StaticViewItem : ViewItem
    {
        private const float MIN_SCALE = 0.65f;
        private const float MAX_SCALE = 1f;

        private FrameModule _frameModule;

        private Vector2 _screenCenter;        

        private Rectangle _screenRectangle = new Rectangle();
        public Rectangle ScreenRectangle
        {
            get
            {
                _screenRectangle.Width = _frameModule.Bound.Width;
                _screenRectangle.Height = _frameModule.Bound.Height;
                _screenRectangle.X = (int)(_position.X - _screenRectangle.Width / 2);
                _screenRectangle.Y = (int)(_position.Y - _screenRectangle.Height / 2);
                return _screenRectangle;
            }
        }

        public StaticViewItem(Game game, FrameModule frameModule)
            : base(game)
        {
            _frameModule = frameModule;            
            _screenCenter = new Vector2(game.GraphicsDevice.Viewport.Width / 2, game.GraphicsDevice.Viewport.Height / 2);
        }

        public StaticViewItem(Game game, FrameModule frameModule, object tag)
            : base(game)
        {
            _frameModule = frameModule;
            _screenCenter = new Vector2(game.GraphicsDevice.Viewport.Width / 2, game.GraphicsDevice.Viewport.Height / 2);
            Tag = tag;
        }

        public override void HandleInput(GameTime gameTime, GameStateManagement.InputState input)
        {
            /*Check for selected event*/
            if (Math.Abs(_position.X - _screenCenter.X) > 0.1f*_screenCenter.X)
                return;

            foreach (GestureSample gesture in input.Gestures)
            {
                if (gesture.GestureType == GestureType.Tap)
                {
                    if (MathLibraries.MathLibraries.CheckPointInRectangle(ScreenRectangle,gesture.Position))
                    {
                        OnItemSelected();
                    }
                }
            }

            base.HandleInput(gameTime, input);
        }       

        public override void Update(GameTime gameTime)
        {
            float dCenter = Math.Abs(_position.X - CenterItemPosition.X);

            _frameModule.Scale = (1- dCenter / ItemDistance)*(MAX_SCALE - MIN_SCALE) + MIN_SCALE;

            _frameModule.Position = _position;
           
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            _frameModule.Draw();
            base.Draw(gameTime);
        }

        public override void PositionChange(Vector2 position)
        {
            _frameModule.Position = position;
            base.PositionChange(position);
        }

        public override Rectangle GetBound()
        {
            return _frameModule.Bound;
        }

        public override float GetComputeCenterData()
        {
            return _frameModule.Scale;
        }
    }
}
